package com.thti.mecool.client.activity;

import java.util.ArrayList;
import java.util.List;

import com.thti.mecool.client.base.BaseActivity;
import com.thti.mecool.client.base.Cache;
import com.thti.mecool.client.command.send.system.GetSend;
import com.thti.mecool.client.model.biz.MessagesBizImpl;
import com.thti.mecool.client.model.po.MobileMessages;

import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.GestureDetector.OnDoubleTapListener;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ViewFlipper;

public class ImportMsgDialog extends BaseActivity implements OnGestureListener,OnDoubleTapListener{
	ViewFlipper ImportMsgView;
	Button okMsg;
	GestureDetector detector;
	ArrayList<View> MsgViews;
	MessagesBizImpl messagesBizImpl;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		noTitle();
		Cache.add(this);
//		View v=LayoutInflater.from(this).inflate(R.layout.mecool_import_msg_dialog, null);
		//		v.setLayoutParams(new LinearLayout.LayoutParams(dm.widthPixels/4*3, dm.heightPixels/2));
		setContentView(R.layout.mecool_import_msg_dialog);
		LinearLayout import_dialog=(LinearLayout)findViewById(R.id.import_dialog);
		DisplayMetrics dm=new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);
		import_dialog.setLayoutParams(new FrameLayout.LayoutParams(dm.widthPixels/4*3, dm.heightPixels/2));
		detector=new GestureDetector(this);
		MsgViews=new ArrayList<View>();
		ImportMsgView=(ViewFlipper)findViewById(R.id.mecool_import_msg_flipper);
		messagesBizImpl=new MessagesBizImpl(this);
	}
	
	private void getData(){
		MsgViews.clear();
		ImportMsgView.removeAllViews();
//		Intent getMsg=getIntent();
//		String contentStr=getMsg.getStringExtra("content");
		List<MobileMessages> messages=messagesBizImpl.findUngent();
		for(int i=0;i<messages.size();i++){
			View view=LayoutInflater.from(this).inflate(R.layout.mecool_import_msg_dialog_item, null);
			TextView content=(TextView)view.findViewById(R.id.mecool_import_msg_item_text);
			TextView title=(TextView)view.findViewById(R.id.mecool_import_msg_item_title);
			title.setText(messages.get(i).getHead());
			content.setText(messages.get(i).getBody());
			okMsg=(Button)view.findViewById(R.id.mecool_import_msg_item_btn);
			okMsg.setOnClickListener(RemoveViewListener);
			view.setTag(messages.get(i));
			MsgViews.add(view);
		}

		if(MsgViews!=null){
			for(View v:MsgViews){
				ImportMsgView.addView(v);
			}
		}
		
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if(keyCode==4){
			return true;
		}
		return super.onKeyDown(keyCode, event);
	}
	
	@Override
	protected void onResume() {
		super.onResume();
getData();
	}
	
	private OnClickListener RemoveViewListener=new OnClickListener() {
		@Override
		public void onClick(View v) {
			if(ImportMsgView.getChildCount()>1){
				ImportMsgView.removeView((View) v.getParent());
			}else{
				finish();
			}
			String id=String.valueOf(((MobileMessages)((View) v.getParent()).getTag()).getId());
			messagesBizImpl.upUrgent(id);
			GetSend.getMessage(id);
		}
	};
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return this.detector.onTouchEvent(event);
	}
	@Override
	public boolean onDoubleTap(MotionEvent e) {
		if(ImportMsgView.isFlipping()) {
			ImportMsgView.stopFlipping();    
	    }else {
	    	ImportMsgView.startFlipping();
	    }
	    return true;
	}

	@Override
	public boolean onDoubleTapEvent(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onSingleTapConfirmed(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
	       if(e1.getX() > e2.getX()) {
	    	   ImportMsgView.setInAnimation(getApplicationContext(), R.anim.left_in);
	    	   ImportMsgView.setOutAnimation(getApplicationContext(),R.anim.left_out);
	    	   ImportMsgView.showPrevious();
	    	   ImportMsgView.stopFlipping();
	       }else if(e1.getX() < e2.getX()) {
	    	   ImportMsgView.setInAnimation(getApplicationContext(), R.anim.right_in);    
	    	   ImportMsgView.setOutAnimation(getApplicationContext(), R.anim.right_out);  
	    	   ImportMsgView.showNext();
	    	   ImportMsgView.stopFlipping();
	       }else {
	           return false;    
	       }    
	       return true;   
	}

	@Override
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}

}
